package com.lalosoft.gastostarjetaweb.util;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.lalosoft.gastostarjetaweb.Constants;

public class EmailSender {
	
	private static final Logger log = Logger.getLogger("EmailSender");
	
	private Session session;
	private String to;
	private String from;
	
	public EmailSender(String from, String to){
		this.from = from;
		this.to = to;
		Properties props = new Properties();
		session = Session.getDefaultInstance(props, null);
	}
	
	public void send(String subject, String body) throws UnsupportedEncodingException, MessagingException{
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from, Constants.ALIAS));
        msg.addRecipient(Message.RecipientType.TO,
                          new InternetAddress(to, ""));
        msg.setSubject(subject);
        msg.setText(body);
        Transport.send(msg);
        log.log(Level.WARNING, "Email enviado correctamente");
	}

}
